package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.StockOut;
import com.neusoft.ecommerce.entity.Storage;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.StockOutService;
import com.neusoft.ecommerce.service.StorageService;

/**
 * 
 * @author maomao
 *
 */
@RestController
public class StockOutController {
	
	private final StockOutService stockOutService;
	private final GoodsService goodsService;
	private final StorageService storageService;
	
	public StockOutController(StockOutService stockOutService, GoodsService goodsService,StorageService storageService) {
		this.stockOutService = stockOutService;
		this.goodsService = goodsService;
		this.storageService = storageService;
	}
	
	@GetMapping("/stockOut_page")
    public ModelAndView stockPage() {
        return new ModelAndView("stockOut/stockOut-list");
    }
	
    @RequestMapping("/stockOut/list")
    @ResponseBody
    public LayuiPager<StockOut> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<StockOut> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = stockOutService.findTotal(pager);
        List<StockOut> list = stockOutService.findList(pager);
        for (StockOut stockOut : list) {
            System.out.println(stockOut);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addstockOut")
    public ModelAndView addstock() {
        return new ModelAndView("stockOut/stockOut-add");
    }
	
	@RequestMapping("/stockOut/storageList")
    public List<Storage> findStorageList() {
	    return storageService.findAll();
    }
	
	@RequestMapping("/stockOut/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
	
	@PostMapping("/stockOut/add")
    public Map<String, Object> add(StockOut stockOut) {
        System.out.println(stockOut);
        Map<String, Object> map = new HashMap<>(16);
        if (stockOutService.add(stockOut)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/stockOut/edit_page")
    public ModelAndView editPage(StockOut stockOut, Model model) {
        System.out.println(stockOut);
        LayuiPager<StockOut> pager = new LayuiPager<>(1,1);
        pager.setCondition(stockOut);
        StockOut message = stockOutService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("stockOut/stockOut-edit");
        model.addAttribute("stockOut", message);
        return view;
    }
	

	@PostMapping("/stockOut/edit")
    public Map<String, Object> edit(StockOut stockOut) {
        System.out.println(stockOut);
        Map<String, Object> map = new HashMap<>(16);
        if (stockOutService.edit(stockOut)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/stockOut/delete")
    public Map<String, Object> delete(StockOut stockOut) {
        System.out.println(stockOut);
        Map<String, Object> map = new HashMap<>(16);
        if (stockOutService.delete(stockOut)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
